package lib.bone.nepudata.service;

import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.nepudata.domain.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@Deprecated
public class ResearchServiceCtl {
    @Autowired(required = false)
    private List<ResearchService> researchServiceList;

    /**
     * 新建记录
     * @param o
     * @return
     */
    public Object saveOne(Object o){
        for (ResearchService service:researchServiceList){
            log.info(service.getServiceType());
            if (service.getServiceType().equals(ResearchTypeConst.AWARD) && o instanceof NepuAward){
                return service.saveOne(o);
            }
            else if (service.getServiceType().equals(ResearchTypeConst.PAPER) && o instanceof NepuPaper){
                return service.saveOne(o);
            }
            else if (service.getServiceType().equals(ResearchTypeConst.BOOK) && o instanceof NepuBook){
                return service.saveOne(o);
            }
            else if (service.getServiceType().equals(ResearchTypeConst.THINKTANK) && o instanceof NepuThinkTank){
                return service.saveOne(o);
            }
            else if (service.getServiceType().equals(ResearchTypeConst.TOPIC) && o instanceof NepuTopic){
                return service.saveOne(o);
            }
            else if (service.getServiceType().equals(ResearchTypeConst.PROPERTY) && o instanceof NepuProperty){
                return service.saveOne(o);
            }
            else if (service.getServiceType().equals(ResearchTypeConst.SUBJECT) && o instanceof NepuSubject){
                return service.saveOne(o);
            }
        }
        throw new BusinessException(ResponseCodeConst.SERVICE_UNAVAILABLE);
        //return ResponseDTO.wrap(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }

    /**
     * 获取项目唯一编码
     * @return
     */
    public String getProjectCode(Object s,String serviceType){
        for (ResearchService service:researchServiceList){
            if (service.getServiceType().equals(serviceType)) {
                return service.getProjectCode(s);
            }
        }
        throw new BusinessException(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }


    /**
     * 更新记录
     * @param o
     * @return
     */
    public Object updateOne(Object o,String serviceType){
        for (ResearchService service:researchServiceList){
            if (service.getServiceType().equals(serviceType)) {
                return service.updateOne(o);
            }
        }
        throw new BusinessException(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }

    /**
     * 根据ID 检索
     * @param o
     * @return
     */
    public Optional<Object> getById(Object o, String serviceType){
        for (ResearchService service:researchServiceList){
            if (service.getServiceType().equals(serviceType)) {
                return service.getById(o);
            }
        }
        throw new BusinessException(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }



    /**
     * 根据ID 删除
     * @param o
     * @return
     */
    public ResponseDTO<Object> deleteById(Object o,String serviceType){
        System.out.println("进入实体删除事件！");
        for (ResearchService  service:researchServiceList){
            if (service.getServiceType().equals(serviceType)) {
                System.out.println("服务类型"+service.getServiceType());
                return ResponseDTO.succData(service.deleteById(o));
            }
        }
        log.info("类型选择结束！");
        return ResponseDTO.wrap(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }
    public PageResultDTO<Object> getAll(Pageable pageable, Example example, String serviceType){
        for (ResearchService service:researchServiceList){
            if (service.getServiceType().equals(serviceType)){
                return service.getAll(pageable,example);
            }
        }
        throw new BusinessException(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }

}
